home *** CD-ROM | disk | FTP | other *** search
- /*
- *
- * menus.c
- *
- * Menu handling routines and utilities.
- *
- *
- * Author: Rob Johnston
- * Date: Wednesday, February 26, 1992
- *
- * Copyright © 1992 Apple Computer, Inc.
- *
- */
-
- #include <Drag.h>
- #include "globals.h"
- #include "prototypes.h"
- #include "resources.h"
-
-
- /*
- * Given a range of menu items and a menu, FindCheckItem returns the
- * item number of the first checked item. If no items in the range
- * are checked, FindCheckItem returns 0 (zero).
- */
-
- short FindCheckItem(MenuHandle theMenu, short rangeStart, short rangeEnd)
-
- { short theMark;
-
- while (rangeStart <= rangeEnd) {
- GetItemMark(theMenu, rangeStart, &theMark);
- if (theMark == checkMark)
- return(rangeStart);
- rangeStart++;
- }
-
- return(0);
- }
-
-
- /*
- * Check1Item checks a single menu item in a range of menu items. theItem is
- * the item number to check. rangeStart and rangeEnd denote a range of menu
- * items that theItem belongs to. Check1Item checks theItem and removes
- * any checks from the other items in the given range. Check1Item returns
- * the item number of the checked item if a newly checked item is selected.
- * If theItem was already checked or theItem was outside of the given range,
- * Check1Item returns 0 (zero).
- */
-
- short Check1Item(MenuHandle theMenu, short theItem, short rangeStart, short rangeEnd)
-
- { short theMark;
-
- if ((theItem < rangeStart) || (theItem > rangeEnd))
- return(0);
-
- while (rangeStart <= rangeEnd) {
- if (rangeStart != theItem)
- CheckItem(theMenu, rangeStart, false);
- rangeStart++;
- }
-
- GetItemMark(theMenu, theItem, &theMark);
-
- if (theMark != checkMark) {
- CheckItem(theMenu, theItem, true);
- return(theItem);
- } else {
- return(0);
- }
- }
-
-
- /*
- * ItemCheck returns true if the given menu item is checked, false otherwise.
- */
-
- short ItemCheck(MenuHandle theMenu, short theItem)
-
- { short theMark;
-
- GetItemMark(theMenu, theItem, &theMark);
- return(theMark == checkMark);
-
- }
-
-
- /*
- * ToggleCheck toggles the check mark on the given menu item. ToggleCheck
- * returns true if the item becomes checked and false if the item becomes
- * unchecked.
- */
-
- short ToggleCheck(MenuHandle theMenu, short theItem)
-
- { short theMark;
-
- GetItemMark(theMenu, theItem, &theMark);
- CheckItem(theMenu, theItem, theMark = (theMark != checkMark));
- return(theMark);
- }
-
-
- /*
- * SetItemEnable enables or disables a menu item depending on the value
- * of the enable parameter.
- */
-
- void SetItemEnable(MenuHandle theMenu, short theItem, short enable)
-
- {
- if (enable) {
- EnableItem(theMenu, theItem);
- } else {
- DisableItem(theMenu, theItem);
- }
- }
-
-
- /*
- * Given a string and a menu, ItemStringToItem will return the item number
- * of the last item that matches the string.
- */
-
- short ItemStringToItem(Str255 theItemString, MenuHandle theMenu)
-
- { short index;
- Str255 theString;
-
- index = CountMItems(theMenu);
- while (index) {
- GetItem(theMenu, index, &theString);
- if (PStrCmp(&theString, theItemString))
- return(index);
- index--;
- }
- return(0);
- }
-
-
- short FontToItem(short fontFamilyNumber)
-
- { Str255 fontName;
-
- GetFontName(fontFamilyNumber, &fontName);
- return(ItemStringToItem(fontName, GetMHandle(idFontMenu)));
- }
-
-
- short SizeToItem(short theSize)
-
- { Str255 theString;
-
- if (theSize == 9)
- return(1);
- NumToString((long) theSize, &theString);
- return(ItemStringToItem(theString, GetMHandle(idSizeMenu)));
- }
-
-
- short ItemToSize(short theItem)
-
- { long theSize;
- Str255 theString;
-
- if (theItem == 1)
- return(9);
- GetItem(GetMHandle(idSizeMenu), theItem, &theString);
- StringToNum(&theString, &theSize);
- return((short) theSize);
- }
-
-
- /*
- * PrepareMenus is called before the user pulls down a menu from the menu
- * bar or types a command key equivalent. PrepareMenus enables and disables
- * menu items within the current program context. Any other menu related
- * setup may be performed here.
- */
-
- void PrepareMenus()
-
- { MenuHandle theMenu;
- Document *theDocument;
- short teSelection, theMode, index;
- TextStyle theStyle;
- Str255 theStr;
-
- theDocument = IsDocumentWindow(FrontWindow());
- teSelection = (theDocument) &&
- ((**(theDocument->theTE)).selStart !=
- (**(theDocument->theTE)).selEnd);
-
- theMenu = GetMHandle(idFileMenu);
-
- SetItemEnable(theMenu, NewItem, gDocumentCount < MaxDocumentCount);
- SetItemEnable(theMenu, OpenItem, gDocumentCount < MaxDocumentCount);
-
- SetItemEnable(theMenu, CloseItem, theDocument != 0L);
- SetItemEnable(theMenu, SaveItem, (theDocument) && (theDocument->dirty));
- SetItemEnable(theMenu, SaveAsItem, theDocument != 0L);
- SetItemEnable(theMenu, RevertItem, (theDocument) &&
- (theDocument->dirty) &&
- (theDocument->fRefNum));
-
- SetItemEnable(theMenu, PageSetupItem, false);
- SetItemEnable(theMenu, PrintItem, false);
-
- theMenu = GetMHandle(idEditMenu);
-
- GetIndString(theStr, MenuStringsID, gCanUndoDrag);
- SetItem(theMenu, iUndo, theStr);
- SetItemEnable(theMenu, iUndo, gCanUndoDrag != slCantUndo);
-
- SetItemEnable(theMenu, iCut, teSelection);
- SetItemEnable(theMenu, iCopy, teSelection);
- SetItemEnable(theMenu, iPaste, theDocument != 0L);
- SetItemEnable(theMenu, iClear, teSelection);
- SetItemEnable(theMenu, iSelectAll, theDocument != 0L);
- SetItemEnable(theMenu, iShowClipboard, false);
-
- theMenu = GetMHandle(idFontMenu);
- SetItemEnable(theMenu, 0, theDocument != 0L);
- if (gFontItem)
- CheckItem(theMenu, gFontItem, false);
- gFontItem = 0;
-
- theMenu = GetMHandle(idSizeMenu);
- SetItemEnable(theMenu, 0, theDocument != 0L);
- if (gSizeItem)
- CheckItem(theMenu, gSizeItem, false);
- gSizeItem = 0;
- index = CountMItems(theMenu);
- while (index) {
- SetItemStyle(theMenu, index, normal);
- index--;
- }
-
- theMenu = GetMHandle(idStyleMenu);
- SetItemEnable(theMenu, 0, theDocument != 0L);
- index = CountMItems(theMenu);
- while (index) {
- CheckItem(theMenu, index--, false);
- }
-
- if (theDocument) {
- theMode = doFont + doFace + doSize;
- TEContinuousStyle(&theMode, &theStyle, theDocument->theTE);
-
- if (theMode & doFont) {
- CheckItem(GetMHandle(idFontMenu),
- gFontItem = FontToItem(theStyle.tsFont), true);
-
- theMenu = GetMHandle(idSizeMenu);
- index = CountMItems(theMenu);
- while (index) {
- if (RealFont(theStyle.tsFont, ItemToSize(index)))
- SetItemStyle(theMenu, index, outline);
- index--;
- }
- }
-
- theMenu = GetMHandle(idSizeMenu);
- if (theMode & doSize) {
- CheckItem(theMenu, gSizeItem = SizeToItem(theStyle.tsSize), true);
- }
-
- theMenu = GetMHandle(idStyleMenu);
- if (theMode & doFace) {
- if (! theStyle.tsFace)
- CheckItem(theMenu, iPlainText, true);
- if (theStyle.tsFace & bold)
- CheckItem(theMenu, iBold, true);
- if (theStyle.tsFace & italic)
- CheckItem(theMenu, iItalic, true);
- if (theStyle.tsFace & underline)
- CheckItem(theMenu, iUnderline, true);
- if (theStyle.tsFace & outline)
- CheckItem(theMenu, iOutline, true);
- if (theStyle.tsFace & shadow)
- CheckItem(theMenu, iShadow, true);
- if (theStyle.tsFace & extend)
- CheckItem(theMenu, iExtended, true);
- if (theStyle.tsFace & condense)
- CheckItem(theMenu, iCondensed, true);
- }
- }
- }
-
-
- short DoSpecialPaste(Document *theDocument)
-
- { long size, offset;
- Handle theData;
-
- size = GetScrap(0L, 'UPRC', &offset);
-
- if (size <= 0)
- return(0);
-
- theData = NewHandle(size);
- GetScrap(theData, 'UPRC', &offset);
-
- HLock(theData);
- PutScrap(size, 'test', *theData);
-
- HUnlock(theData);
- DisposeHandle(theData);
- }
-
-
-
- /*
- * DoMenuCommand dispatches menu command routines from a menu select parameter.
- * This function is called when the user selects a menu item with the mouse
- * or types a command key equivalent.
- */
-
- void DoMenuCommand(long select)
-
- { short theMenuID, theItem, theFontNumber, result;
- MenuHandle theMenu;
- Str255 theName;
- WindowPtr theWindow;
- Document *theDocument;
- Point where;
- RGBColor outColor;
-
- theDocument = IsDocumentWindow(theWindow = FrontWindow());
-
- theItem = LoWord(select);
- theMenuID = HiWord(select);
- theMenu = GetMHandle(theMenuID);
- switch(theMenuID) {
- case idAppleMenu:
- switch(theItem) {
- case AboutItem:
- result = Alert(256, 0L);
- break;
- default:
- GetItem(GetMHandle(idAppleMenu), theItem, &theName);
- OpenDeskAcc(theName);
- }
- break;
- case idFileMenu:
- switch(theItem) {
- case NewItem:
- DoNewDocument();
- PrepareMenus();
- DrawMenuBar();
- break;
- case OpenItem:
- DoOpenDocument();
- PrepareMenus();
- DrawMenuBar();
- break;
- case CloseItem:
- if (theDocument) {
- CloseDocument(theDocument);
- PrepareMenus();
- DrawMenuBar();
- }
- break;
- case SaveItem:
- if (theDocument)
- DoSaveDocument(theDocument);
- break;
- case SaveAsItem:
- if (theDocument)
- DoSaveAsDocument(theDocument);
- break;
- case RevertItem:
- DisableUndoDrag();
- if (theDocument)
- DoRevertDocument(theDocument);
- break;
- case QuitItem:
- gQuitting = true;
- while ((gQuitting) && (theDocument = IsDocumentWindow(FrontWindow()))) {
- CloseDocument(theDocument);
- }
- if (gQuitting)
- gQuit = true;
- break;
- }
- break;
- case idEditMenu:
- switch(theItem) {
- case iUndo:
- DoUndoDrag();
- break;
- case iCut:
- if (theDocument) {
- DisableUndoDrag();
- TEICut(theDocument->theTE);
- }
- break;
- case iCopy:
- if (theDocument) {
- TECopy(theDocument->theTE);
- }
- break;
- case iPaste:
- if (theDocument) {
- if (!DoSpecialPaste(theDocument)) {
- DisableUndoDrag();
- TEIPaste(theDocument->theTE, 0L, 0L);
- }
- }
- break;
- case iClear:
- if (theDocument) {
- DisableUndoDrag();
- TEDelete(theDocument->theTE);
- }
- break;
- case iSelectAll:
- if (theDocument)
- DoSelectAllDocument(theDocument);
- break;
- }
- break;
- case idFontMenu:
- DisableUndoDrag();
- GetItem(GetMHandle(idFontMenu), theItem, &theName);
- GetFNum(&theName, &theFontNumber);
- DoFontSelection(theFontNumber);
- break;
- case idSizeMenu:
- DisableUndoDrag();
- DoSizeSelection(ItemToSize(theItem));
- break;
- case idStyleMenu:
- DisableUndoDrag();
- DoStyleSelection(theItem);
- break;
- }
-
- if (theDocument = IsDocumentWindow(FrontWindow()))
- TEGetHiliteRgn(theDocument->hiliteRgn, theDocument->theTE);
-
- HiliteMenu(0);
- }
-
-